package com.yctan.base.utils;


import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.util.UUID;

/**
 * 加密算法类
 */
@Component
public class EncryptionUtils {

    private static String SECRET_KEY;

    @Value("${EncryptionUtils.key}")
    public void setSECRET_KEY(String SECRET_KEY) {
        EncryptionUtils.SECRET_KEY = SECRET_KEY;
    }

    private static final String DES = "DES";

    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    /**
     * 使用MD5摘要算法加密
     *
     * @param password 原密码
     * @param salt     盐值
     * @return 加密后密码
     */
    public static String getMd5Password(String password, String salt) {
        String pwd = salt + password + salt;
        for (int i = 0; i < 5; i++) {
            pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        }
        return pwd;
    }

    /**
     * 对字符加星号处理：除前面几位和后面几位外，其他的字符以星号代替
     *
     * @param content  传入的字符串
     * @param frontNum 保留前面字符的位数
     * @param endNum   保留后面字符的位数
     * @return 带星号的字符串
     */
    public static String replaceString2Star(String content, int frontNum, int endNum) {
        if (content == null || content.trim().isEmpty())
            return content;

        int len = content.length();

        if (frontNum >= len || frontNum < 0 || endNum >= len || endNum < 0)
            return content;

        if (frontNum + endNum >= len)
            return content;
        int beginIndex = frontNum;
        int endIndex = len - endNum;
        char[] cardChar = content.toCharArray();
        for (int j = beginIndex; j < endIndex; j++) {
            cardChar[j] = '*';
        }
        return new String(cardChar);
    }

    /**
     * 获取秘钥对象
     *
     * @return
     * @throws Exception
     */
    private static final SecretKey getSecretKeyFactory() throws Exception {
        SecretKeyFactory des = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = des.generateSecret(new DESKeySpec(SECRET_KEY.getBytes()));
        return secretKey;
    }

    /**
     * 加密
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static final String encryption(String param) {
        try {
            Cipher cipher = Cipher.getInstance(DES);
            SecretKey secretKey = getSecretKeyFactory();
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return new String(Base64.encodeBase64(cipher.doFinal(param.toString().getBytes())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 解密
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static final String decrypt(String value)  {
        try {
            Cipher cipher = Cipher.getInstance(DES);
            SecretKey secretKey = getSecretKeyFactory();
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.decodeBase64(value.getBytes())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public static void main(String[] args) {
        String a = uuid();
        String pwd = getMd5Password("123456", a);
        System.out.println(a);
        System.out.println(pwd);
    }

}
